﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IHISCETeim.DAL;
using IHISCETeim.Model;
using IHISCETeim.Library;
using System.Data;

namespace IHISCETeim.BLL
{
    public class SAPConfigController: BaseController
    {
        private static List<SAPConfigInfo> _configs;
        private static List<String> changedKeys = new List<string>();

        #region 初始化操作

        private static List<SAPConfigInfo> Configs
        {
            get
            {
                if (_configs == null)
                {
                    InitConfigs();
                }
                return _configs;
            }
        }
        private static void InitConfigs()
        {
            _configs = CBO.FillCollection<SAPConfigInfo>(SAPConfigDAL.GetAll());
        }

        private static void Reload()
        {
            _configs = null;
        }

        /// <summary>
        /// 获取配置信息
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetValue(string key)
        {
            return Configs.Where(c => c.ConfigKey == key).Select(c => c.ConfigValue).FirstOrDefault();
        }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        private static void SetValue(string key, string value)
        {
            SAPConfigInfo config = Configs.Where(c => c.ConfigKey == key).FirstOrDefault();
            if (config != null)
            {
                if (config.ConfigValue != value)
                {
                    changedKeys.Add(key);
                    config.ConfigValue = value;
                }
            }
            else
            {
                SAPConfigInfo obj = new SAPConfigInfo();
                obj.ConfigKey = key;
                obj.ConfigValue = value;
                Configs.Add(obj);
            }
        }

        #endregion

        #region 实际使用项

        /// <summary>
        /// 保存所有更改的配置项
        /// </summary>
        public static bool SaveAll()
        {

            if (SAPConfigDAL.Update(Configs))
            {
                Reload();
                return true;
            }
            else
            {
                Reload();
                return false;
            }

        }
        /// <summary>
        /// 获取借件部门对应的邮箱
        /// </summary>
        /// <param name="deptName"></param>
        /// <returns></returns>
        public static string SAPJieJian_GetDeptEmail(string deptName)
        {
            return GetValue("SAPJieJian_"+ deptName+ "Email");
        }

        #endregion


    }
}
